import os
from pydub import AudioSegment
from tqdm import tqdm

def convert_m4a_to_mp3(m4a_file, mp3_file):
    audio = AudioSegment.from_file(m4a_file, format='m4a')
    audio.export(mp3_file, format='mp3')

def split_mp3_file(mp3_file, segment_length):
    audio = AudioSegment.from_file(mp3_file, format='mp3')
    total_length = len(audio)
    segment_length_ms = segment_length * 60 * 1000
    segments = []

    for start_time in tqdm(range(0, total_length, segment_length_ms), desc="Splitting"):
        end_time = start_time + segment_length_ms
        segment = audio[start_time:end_time]
        segments.append(segment)

    for i, segment in tqdm(enumerate(segments), desc="Exporting"):
        segment.export(f"segment_{i}.mp3", format='mp3')

# Пример использования функций
m4a_file = "input.m4a"
mp3_file = "output.mp3"
segment_length = 5  # длительность отрезков в минутах

convert_m4a_to_mp3(m4a_file, mp3_file)
split_mp3_file(mp3_file, segment_length)

